Ext.require([
    'Ext.grid.*',
    'Ext.data.*',
    'Ext.panel.*'
]);

var url;

Ext.onReady(function() {
	
	url = APP_PATH + "/getMenuJsonData.action";
	var pid = document.getElementById("menuList_menu_pid").value;
	
	if (pid) {
		
		url += "?pid=" + pid;
	}
	
	Ext.define('GridModel', {
        extend: 'Ext.data.Model',
        fields: ['id', 'name', 'action', 'level', 'sort']
    });
	
	var store = Ext.create('Ext.data.JsonStore', {
        model: 'GridModel',
        proxy: {
            type: 'ajax',
            url: url,
            reader: {
                type: 'json',
                root: 'root'
            }
        }
    });
	
	store.load();
	
	var cm = [{text: 'id', flex: 30, dataIndex: 'id', hidden: true},
	          {text: '菜单名称', flex: 35, dataIndex: 'name'},
	          {text: '菜单事件', flex: 50, dataIndex: 'action'},
	          {text: '菜单级别', flex: 35, dataIndex: 'level'},
	          {text: '显示顺序', flex: 35, dataIndex: 'sort'},
	          {text: '操作', flex: 35, dataIndex: 'id', renderer: renderOption}
	         ];
	
	var tbar = [{text: '添加', iconCls: 'add', handler: uf_Add},
				{text: '重新加载', iconCls: 'refresh', handler: uf_Query},
				{text: '返回', iconCls: 'return', handler: uf_Return}
				];
	
	var listView = Ext.create('Ext.grid.Panel', {
		selType: 'checkboxmodel',
        height: 550,
        frame: true,
        collapsible: true,
        title: '<b>菜单管理</b>',
        iconCls: 'icon-grid',
        renderTo: Ext.get('grid'),
        store: store,
        tbar: tbar,
        multiSelect: true,
        collapsible: false,
        viewConfig: {
            emptyText: '<center>没有查询到相关记录!</center>'
        },
        columns: cm
    });
	
	function uf_Add() {
		
		submitForm("menuNew.action");
	}
	
	function uf_Query() {
		
		store.load();
	}
	
	function renderOption(val, cellmeta, record, rowIndex, columnIndex, store) {
		
		var str = createA('子菜单', 'uf_SubMenu', val);
		str += createA('编辑', 'uf_Edit', val);
		str += createA('删除', 'uf_Del', val);
		
		return str;
	}
});

function uf_Query() {
	
	store.load();
}

function uf_Edit(id) {
	
	submitForm("menuEdit.action?menuId=" + id);
}

function uf_Del(id) {
	
	if(!confirm('您确认删除该记录吗?')) return false;
	
	Ext.Ajax.request({
    	waitMsg:'正在加载数据，请稍侯……', 
	    url: 'systemUserAjaxDelete.action',
	    method:'POST',
	 	params: {systemUserId: id},
		success:function(result, request){
			if(result.responseText == "") {
				uf_Query();
			} else {
				alert(result.responseText);
			}
		} 
	});
}

function uf_Return(id) {
	
	submitForm("menuList.action");
}

function uf_SubMenu(id) {
	
	document.getElementById("menuList_menu_pid").value = id;
	
	submitForm("menuList.action");
}